﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;


namespace eVoting
{
    public partial class Vote1 : System.Web.UI.Page
    {
        DBManager db;
        int selectedPollID;
        int selectedCandidateID;
        int userID = 7; //provided by maged
        User user = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new DBManager();
            user = db.getUserById(userID);
            messageLabel.Visible = false;
 
            if (!IsPostBack)
            {
                List<Poll> polls = db.getAssignedPollsByUserID(userID);
                if (polls.Count == 0)
                {
                    messageLabel.Text = "No polls are available for you yet.";
                    messageLabel.Visible = true;
                    PollList.Enabled = false;
                }
                PollList.Items.Add("Select a poll");
                
                
                for (int i = 0; i < polls.Count; i++)
                {
                    PollList.Items.Add(new ListItem(polls[i].name, polls[i].ID + ""));

                }


            }
         
        }

        protected void PollList_SelectedIndexChanged(object sender, EventArgs e)
        {
            db = new DBManager();
            messageLabel.Text = "";
            CandidateList.Items.Clear();
            candidateButton.Visible = false;

            if (PollList.SelectedIndex != 0)
            { 
                selectedPollID = int.Parse(PollList.SelectedItem.Value);
                if (db.hadVoted(userID, selectedPollID) == false)
                {
                    
                    pollCandidates();
                    candidateButton.Visible = true;
                }
                else
                {
                    messageLabel.Text = "You already Voted!";
                    messageLabel.Visible = true;
                }

                init_votes_table();
           }

        }

        protected void pollCandidates()
        {
            db = new DBManager();
            List<Candidate> candidates = db.getCandidatesByPoll(selectedPollID);
            for(int i=0; i < candidates.Count; i++)
            {
                CandidateList.Items.Add(new ListItem(candidates[i].firstName +" "+ candidates[i].lastName, candidates[i].ID+""));

            }

            if (candidates.Count > 0)
            {
                candidateButton.Visible = true;
                CandidateList.Visible = true;
            }
                
        }

        protected void CandidateList_SelectedIndexChanged(object sender, EventArgs e)
        {
            selectedCandidateID = int.Parse(CandidateList.SelectedItem.Value);
            //Otherwise value resets to 0 !!!
            selectedPollID = int.Parse(PollList.SelectedItem.Value); 
        }

        protected void submitCandidate()
        {
            db = new DBManager();
            
            Pailler pailler = new Pailler(selectedPollID); //arg: the ID of the poll
            string selectedCandidate = pailler.encrypt(userID).ToString(); //arg: is the user ID

            bool inserted = db.insertPollUser(userID, selectedPollID, selectedCandidate);
            
            if (inserted)
            {
                            
                messageLabel.Text = "Your vote has been successfully counted :)";
                messageLabel.Visible = true;

                
                //not working :(
                /*TableRow row = new TableRow();
                
                TableCell cell1 = new TableCell();
                cell1.Text = user.firstName +" "+ user.lastName;
                row.Cells.Add(cell1);
                TableCell cell2 = new TableCell();
                cell2.Text = selectedCandidate;
                row.Cells.Add(cell2);

                Table1.Rows.Add(row);*/

                init_votes_table();

                candidateButton.Visible = false;
                CandidateList.Visible = false;
                Utilities.sendMail(user.email + "@student.guc.edu.eg", "Dear " + user.firstName + " " + user.lastName + ",\n\n\tKindly be informed that your vote has been counted.\n\tYou voted on " + DateTime.Now.ToString() + "\n\nYour encrypted vote is " + selectedCandidate + "\n\n" + "\n\n Student Union Committee.");

            }
            else
            {
                messageLabel.Text = "Sorry! An Error occured while processing your vote. Please try again.";
                messageLabel.Visible = true;
            }
        }

        protected void candidateButton_Click(object sender, EventArgs e)
        {
            submitCandidate();
        }

        public void init_votes_table()
        {

            List<PollUser> pollUsersVoted = db.getUsersVotedInPoll(selectedPollID);
           
            if (pollUsersVoted.Count != 0)
            {
                TableHeaderRow header = new TableHeaderRow();
                TableHeaderCell usernameCell = new TableHeaderCell();
                usernameCell.Text = "User";

                TableHeaderCell voteCell = new TableHeaderCell();
                voteCell.Text = "Encrypted Vote";

                header.Cells.Add(usernameCell);
                header.Cells.Add(voteCell);

                Table1.Rows.Add(header);

            }
            for (int i = 0; i < pollUsersVoted.Count; i++)
            {
                int uID = pollUsersVoted[i].user;
                User userMatched = db.getUserById(uID);
                TableRow row = new TableRow();
                TableCell cell1 = new TableCell();
                cell1.Text = userMatched.firstName + " " + userMatched.lastName;
                row.Cells.Add(cell1);
                TableCell cell2 = new TableCell();
                cell2.Text = pollUsersVoted[i].candidate + "";
                row.Cells.Add(cell2);
                Table1.Rows.Add(row);
            }
        }
    }
}